﻿using _17bang.Pages;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace _17bang.Filters
{
    //使用[NeedLogOn] 保证未登录用户无法访问只有登录用户才能访问的页面：
    //未登录用户强行访问上述页面，自动跳转到登录页面
    public class NeedLogOnAttribute : Attribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            string userId = context.HttpContext.Request.Cookies[Keys.UserId];
            if (string.IsNullOrEmpty(userId))
            {
                context.Result = new RedirectResult("/Log/On");
            }
        }
    }
}
